#include "stdafx.h"

#include "common.h"

#include "..\..\includes\mfciterators.h"

using namespace Microsoft::VisualStudio::CppUnitTestFramework;

namespace UnitTests
{
   TEST_CLASS(List_UnitTest)
   {
   public:

      TEST_METHOD(Test_CTypedPtrList_CObList)
      {
         CTypedPtrList<CObList, CFooObject*> oblist;
         for(auto v : g_sample)
            oblist.AddTail(new CFooObject(v));

         auto i = 0;
         for(auto o : oblist)
         {
            Assert::AreEqual(o->value, g_sample[i++]);
         }

         TraceTest(oblist, L"%p");
         TraceTestConst(oblist, L"%p");

         for(auto& o : oblist)
         {
            (static_cast<CFooObject*>(o))->value *= 2;
         }

         i = 0;
         for(auto const & o : oblist)
         {
            Assert::AreEqual(o->value, g_sample[i++] * 2);
         }

         auto const & coblist = oblist;
         i = 0;
         for(auto const & o : coblist)
         {
            Assert::AreEqual(o->value, g_sample[i++] * 2);
         }

         for(auto o : oblist)
            delete o;
      }

      TEST_METHOD(Test_CTypedPtrList_CPtrList)
      {
         CTypedPtrList<CPtrList, CBar*> ptrlist;
         for(auto v : g_sample)
            ptrlist.AddTail(new CBar(v));

         auto i = 0;
         for(auto o : ptrlist)
         {
            Assert::AreEqual(o->value, g_sample[i++]);
         }

         TraceTest(ptrlist, L"%p");
         TraceTestConst(ptrlist, L"%p");

         for(auto& o : ptrlist)
         {
            o->value *= 2;
         }

         i = 0;
         for(auto const & o : ptrlist)
         {
            Assert::AreEqual(o->value, g_sample[i++] * 2);
         }

         for(auto o : ptrlist)
            delete o;
      }

      TEST_METHOD(Test_CObList)
      {
         CObList oblist;
         for(auto v : g_sample)
            oblist.AddTail(new CFooObject(v));

         auto i = 0;
         for(auto o : oblist)
         {
            Assert::AreEqual((static_cast<CFooObject*>(o))->value, g_sample[i++]);
         }

         TraceTest(oblist, L"%p");
         TraceTestConst(oblist, L"%p");

         for(auto& o : oblist)
         {
            (static_cast<CFooObject*>(o))->value *= 2;
         }

         i = 0;
         for(auto const & o : oblist)
         {
            Assert::AreEqual((static_cast<CFooObject*>(o))->value, g_sample[i++] * 2);
         }

         for(auto o : oblist)
            delete o;
      }

      TEST_METHOD(Test_CPtrList)
      {
         CPtrList ptrlist;
         for(auto v : g_sample)
            ptrlist.AddTail(new CBar(v));

         auto i = 0;
         for(auto o : ptrlist)
         {
            Assert::AreEqual((static_cast<CBar*>(o))->value, g_sample[i++]);
         }

         TraceTest(ptrlist, L"%p");
         TraceTestConst(ptrlist, L"%p");

         for(auto& o : ptrlist)
         {
            (static_cast<CBar*>(o))->value *= 2;
         }

         i = 0;
         for(auto const & o : ptrlist)
         {
            Assert::AreEqual((static_cast<CBar*>(o))->value, g_sample[i++] * 2);
         }

         for(auto o : ptrlist)
            delete o;
      }

      TEST_METHOD(Test_CStringList)
      {
         CStringList strlist;

         for(auto const & v : g_sample_str)
            strlist.AddTail(v);

         TraceTest(strlist, L"%s");
         TraceTestConst(strlist, L"%s");

         auto i = 0;
         for(auto v : strlist)
         {
            Assert::AreEqual(v, g_sample_str[i++]);
         }

         for(auto& v : strlist)
            v.MakeUpper();

         i = 0;
         for(auto v : strlist)
         {
            Assert::AreEqual(v, g_sample2_str[i++]);
         }
      }

      TEST_METHOD(Test_CList_int)
      {
         CList<int> list;

         for(auto v : g_sample)
            list.AddTail(v);

         TestNumeric<CList<int>, int>(list, g_sample);
         TestNumericConst<CList<int>, int>(list, g_sample);

         for(auto& v : list)
            v *= 2;

         TestNumeric<CList<int>, int>(list, g_sample2);
         TestNumericConst<CList<int>, int>(list, g_sample2);
      }

      TEST_METHOD(Test_CList_CString)
      {
         CList<CString> strlist;

         for(auto const & v : g_sample_str)
            strlist.AddTail(v);

         TraceTest(strlist, L"%s");
         TraceTestConst(strlist, L"%s");

         auto i = 0;
         for(auto v : strlist)
         {
            Assert::AreEqual(v, g_sample_str[i++]);
         }

         for(auto& v : strlist)
            v.MakeUpper();

         i = 0;
         for(auto v : strlist)
         {
            Assert::AreEqual(v, g_sample2_str[i++]);
         }
      }
   };
}
